﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.TES.Entity.FunctionEntity;
using ZDSoft.TES.Core;
using ZDSoft.TES.Service.FunctionService;
using ZDSoft.TES.Web.Helpers;
using NHibernate.Criterion;

namespace ZDSoft.TES.Web.Controllers
{
    public class QuotaController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult getAllQuota()
        {
            IList<Quota> list = Container.Instance.Resolve<IQuotaService>().FindAll();
            IList<ZTreeHelper<Quota>.ZTreeNode> tree = new List<ZTreeHelper<Quota>.ZTreeNode>();
            foreach (Quota entity in list)
            {
                if (entity.Parent == null)
                {
                    ZTreeHelper<Quota>.ZTreeNode node = new ZTreeHelper<Quota>.ZTreeNode();
                    node.id = entity.ID;
                    node.tld = entity.Percentage.ToString();
                    node.isParent = true;
                    node.pid = 0;
                    node.name = entity.QuotaName;
                    node.children = new List<ZTreeHelper<Quota>.ZTreeNode>();
                    foreach (Quota chi in entity.Children)
                    {
                        ZTreeHelper<Quota>.ZTreeNode child = new ZTreeHelper<Quota>.ZTreeNode();
                        child.id = chi.ID;
                        child.pid = chi.Parent.ID;
                        child.tld = entity.Percentage.ToString();
                        child.name = chi.QuotaName;
                        child.isParent = true;
                        node.children.Add(child);
                    }
                    tree.Add(node);
                }
            }
            return this.Json(tree, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public void Delete(int id)
        {
            if (id != 0)
            {
                Quota entity = Container.Instance.Resolve<IQuotaService>().FindByID(id);
                if (entity.Children.Count == 0)
                {
                    Container.Instance.Resolve<IQuotaService>().Delete(id);
                }
                else
                {
                    foreach (Quota chi in entity.Children)
                    {
                        Container.Instance.Resolve<IQuotaService>().Delete(chi.ID);
                    }
                    Container.Instance.Resolve<IQuotaService>().Delete(id);
                }
            }
        }

        public string getEntiyID(string name, string percentage)
        {
            IList<ICriterion> conditions = new List<ICriterion>();
            conditions.Add(Expression.Eq("QuotaName", name));
            conditions.Add(Expression.Eq("Percentage", double.Parse(percentage)));
            return Container.Instance.Resolve<IQuotaService>().Find(conditions)[0].ID.ToString() + "," + Container.Instance.Resolve<IQuotaService>().Find(conditions)[0].Percentage.ToString();
        }

        [HttpPost]
        public void Opreate(int id, string name, double percentage, string pId)
        {
            if (id == 0)
            {
                Quota quota = new Quota();
                quota.QuotaName = name;
                quota.Percentage = percentage;
                if (pId == "null" || pId == "0")
                {
                    quota.Parent = null;
                }
                else
                {
                    quota.Parent = Container.Instance.Resolve<IQuotaService>().FindByID(int.Parse(pId));
                }
                Container.Instance.Resolve<IQuotaService>().Create(quota);
            }
            else
            {
                Quota entity = Container.Instance.Resolve<IQuotaService>().FindByID(id);
                entity.QuotaName = name;
                entity.Percentage = percentage;
            }
        }
    }
}
